<?php

$db = new SQLiteDatabase('InstantInput.db');

if ($_GET['action'] == "list_new_messages")
{	
	$result = $db->query("SELECT * FROM Messages WHERE class_id = " . $_GET['uniqueCourseNumber'] . " AND message_id > " . $_GET['mostRecentMessageNumber'] . "");
	
	$messages = array();
	while ($result->valid()) 
	{
		$curr = $result->current();
	
		$message = array(
			"uniqueCourseNumber"=> $curr['class_id'],
			"thread"=>$curr['thread_id'],
			"author"=>$curr['sunet_id'],
			"postTime"=>$curr['post_time'],
			"uniqueMessageNumber"=>$curr['message_id'],
			"numberOfLikes"=>$curr['likes'],
			"message"=>stripcslashes($curr['message'])
		);
		
		$messages[] = $message;
		$result->next();	
	}	
	
	print json_encode($messages);
}

if ($_GET['action'] == "submit_message")
{
	$result = "false";
	$time = sqlite_escape_string(date("D M j G:i:s T Y"));
	$message = sqlite_escape_string($_GET['message']);
	
	$thread_id = $_GET['thread'];
	if ($thread_id == "null")
	{
		$result = $db->query("Insert INTO Threads (sunet_id, class_id, message, post_time) VALUES ('" . $_ENV["WEBAUTH_USER"] . "', " . $_GET['uniqueCourseNumber'] . ", '" . $message . "', '" . $time . "')");
		$threads = $db->query("SELECT thread_id FROM Threads ORDER BY thread_id DESC LIMIT 1");

		$new_thread = $threads->current();  

		$thread_id = $new_thread['thread_id'];
	}	

	$result = $db->query("Insert INTO Messages(sunet_id, class_id, thread_id, post_time, likes, message) VALUES ('" . $_ENV["WEBAUTH_USER"] . "', " . $_GET['uniqueCourseNumber'] . ", " . $thread_id . ", '" . $time . "', 0, '" . $message . "')");
	
	if ($result == false)
		$result = "false";
	else
		$result = "true";
		
	$success = array("success"=> $result);
	print json_encode($success);
}

if ($_GET['action'] == "like_message")
{
	$result = "false";
	
	$db_result = $db->query("SELECT liked_id FROM Liked_Messages WHERE message_id = '" . $_GET['uniqueMessageNumber'] . "' AND sunet_id = '" . $_ENV["WEBAUTH_USER"] . "'");
	if ($db_result->numRows() > 0) 
	{
		$result = "false";
	}
	else
	{
		$db->query("UPDATE Messages SET likes = likes + 1 WHERE message_id = " . $_GET['uniqueMessageNumber']. "");
		$db->queryExec("Insert INTO Liked_Messages (message_id, sunet_id) VALUES (" . $_GET['uniqueMessageNumber'] . ", '" . $_ENV["WEBAUTH_USER"] . "')");
		$result = "true";
	}
	
	$success = array("success"=> $result);
	print json_encode($success);
}

if ($_GET['action'] == "list_new_likes")
{	
	$result = $db->query("SELECT Messages.class_id, Liked_Messages.sunet_id, Messages.thread_id, Liked_Messages.liked_id, Liked_Messages.message_id FROM Liked_Messages, Messages WHERE Messages.class_id = " . $_GET['uniqueCourseNumber'] . " AND Liked_Messages.liked_id > " . $_GET['mostRecentLikedNumber'] . " AND Liked_Messages.message_id = Messages.message_id");
	
	$liked_messages = array();
	while ($result->valid()) 
	{
		$curr = $result->current();
		
		$liked = array(
			"uniqueCourseNumber"=>$curr['Messages.class_id'],
			"author"=>$curr['Liked_Messages.sunet_id'],
			"uniqueLikedNumber"=>$curr['Liked_Messages.liked_id'],
			"uniqueMessageNumber"=>$curr['Liked_Messages.message_id'],
			"uniqueThreadNumber"=>$curr['Messages.thread_id']
		);

		$liked_messages[] = $liked;
		$result->next();	
	}	
	
	print json_encode($liked_messages);
}

?>